# This is my first time using cmake, so bare with me if its all wrong :)
cmake_minimum_required(VERSION 2.6)
project(fud)

SET(CHROME_SRC ~/proj/chrome/trunk/src)
GET_FILENAME_COMPONENT(CHROME_SRC ${CHROME_SRC} ABSOLUTE)

find_package(PythonInterp REQUIRED)
find_package(OpenGL REQUIRED)
find_package(GLUT REQUIRED)

if(WIN32)
  FIND_PATH(GLUT_INCLUDE_PATH GL/glut.h ${PROJECT_SOURCE_DIR}/win32_stuff/include)
  FIND_LIBRARY(GLUT_LIBRARY NAMES freeglut_static PATHS ${PROJECT_SOURCE_DIR}/win32_stuff/lib)
  FIND_LIBRARY(GLEW_LIBRARY NAMES glew32 PATHS ${PROJECT_SOURCE_DIR}/win32_stuff/lib)
endif()

#add_subdirectory(src)

function(bin2arr SRC DST)
add_custom_command(
  OUTPUT ${CMAKE_BINARY_DIR}/${DST}
  COMMAND ${PYTHON_EXECUTABLE} ${PROJECT_SOURCE_DIR}/bin/bin2arr ${CMAKE_SOURCE_DIR}/${SRC} ${CMAKE_BINARY_DIR}/${DST}
  DEPENDS ${CMAKE_SOURCE_DIR}/${SRC}
)
endfunction(bin2arr)

bin2arr(src/tentacle.vtx.txt gen/tentacle.vtx.h)
bin2arr(src/tentacle.frg.txt gen/tentacle.frg.h)
bin2arr(src/textrender.vtx.txt gen/textrender.vtx.h)
bin2arr(src/textrender.frg.txt gen/textrender.frg.h)
bin2arr(src/shader.txt gen/shader.h)
bin2arr(assets/font.raw gen/font.h)

include_directories(${CMAKE_BINARY_DIR}/gen)
include_directories(${OpenGL_INCLUDE_DIRS})
include_directories(${GLUT_INCLUDE_PATH})

if(!WIN32)
  set(CMAKE_CXX_FLAGS "-march=native -ffast-math")
else()
  add_definitions(-D_USE_MATH_DEFINES)
  add_definitions(-DGLEW_STATIC -DFREEGLUT_STATIC)
endif()

add_definitions(-UNDEBUG)
set(CMAKE_BUILD_TYPE RelWithDebInfo)
#set(CMAKE_BUILD_TYPE Debug)

## stand alone exe ##
add_executable(fud
    src/glumain.cpp
    src/test.cpp
    src/tentacle.cpp
    src/textrender.cpp
    src/math3d.cpp
    src/utils.cpp
    src/drawstats.cpp
    ${CMAKE_BINARY_DIR}/gen/tentacle.vtx.h
    ${CMAKE_BINARY_DIR}/gen/tentacle.frg.h
    ${CMAKE_BINARY_DIR}/gen/textrender.vtx.h
    ${CMAKE_BINARY_DIR}/gen/textrender.frg.h
    ${CMAKE_BINARY_DIR}/gen/shader.h
    ${CMAKE_BINARY_DIR}/gen/font.h
)
target_link_libraries(fud ${GLUT_LIBRARY} ${GLEW_LIBRARY} ${OPENGL_LIBRARY})

## trusted ppapi plugin ##
if(IS_DIRECTORY ${CHROME_SRC})
    add_library(fud_ppapi SHARED
        src/ppapimain.cpp
        src/test.cpp
        src/tentacle.cpp
        src/textrender.cpp
        src/math3d.cpp
        src/utils.cpp
        src/drawstats.cpp
        ${CMAKE_BINARY_DIR}/gen/tentacle.vtx.h
        ${CMAKE_BINARY_DIR}/gen/tentacle.frg.h
        ${CMAKE_BINARY_DIR}/gen/textrender.vtx.h
        ${CMAKE_BINARY_DIR}/gen/textrender.frg.h
        ${CMAKE_BINARY_DIR}/gen/shader.h
        ${CMAKE_BINARY_DIR}/gen/font.h

        ${CHROME_SRC}/ppapi/cpp/module.cc
        ${CHROME_SRC}/ppapi/cpp/instance.cc
        ${CHROME_SRC}/ppapi/cpp/resource.cc
        ${CHROME_SRC}/ppapi/cpp/var.cc
        ${CHROME_SRC}/ppapi/cpp/core.cc
        ${CHROME_SRC}/ppapi/cpp/ppp_entrypoints.cc
        ${CHROME_SRC}/ppapi/cpp/input_event.cc
        ${CHROME_SRC}/ppapi/cpp/url_loader.cc
        ${CHROME_SRC}/ppapi/cpp/url_response_info.cc
        ${CHROME_SRC}/ppapi/cpp/file_ref.cc
        ${CHROME_SRC}/ppapi/cpp/dev/graphics_3d_dev.cc
        ${CHROME_SRC}/ppapi/cpp/dev/graphics_3d_client_dev.cc
        ${CHROME_SRC}/ppapi/cpp/dev/context_3d_dev.cc
        ${CHROME_SRC}/ppapi/lib/gl/gles2/gles2.c
        ${CHROME_SRC}/ppapi/lib/gl/gles2/gl2ext_ppapi.c
    )
    include_directories(${CHROME_SRC} ${CHROME_SRC}/ppapi ${CHROME_SRC}/ppapi/lib/gl/include)
    target_link_libraries(fud_ppapi ${OPENGL_LIBRARY})
endif()
